package com.joinval.common.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.joinval.common.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 *
 */
@Data
@Accessors(chain = true)
@ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty("响应是否成功")
	private Boolean success;

	@ApiModelProperty(
			value = "响应码,0表示成功",
			example = "0"
	)
	private Integer code;

	@ApiModelProperty("响应消息")
	private String msg;

	@ApiModelProperty("返回值")
	private T data;

	public Result() {

	}

	public static Result ok() {
		return ok(ResultCodeEnum.OK);
	}

	public static Result ok(String msg) {
		return ok().setMsg(msg);
	}

	public static Result ok(ResultCodeEnum rCodeEnum) {
		Result r = new Result();
		r.setMsg(rCodeEnum.getMsg());
		r.setCode(rCodeEnum.getCode());
		r.setSuccess(rCodeEnum.getSuccess());
		return r;
	}

	public static Result error() {
		return error(ResultCodeEnum.ERROR);
	}

	public static Result error(String msg) {
		return error().setMsg(msg);
	}

	public static Result error(Integer code, String msg) {
		Result r = new Result();
		r.setCode(code);
		r.setMsg(msg);
		return r;
	}

	public static Result error(ResultCodeEnum rCodeEnum) {
		Result r = new Result();
		r.setCode(rCodeEnum.getCode());
		r.setMsg(rCodeEnum.getMsg());
		r.setSuccess(rCodeEnum.getSuccess());
		return r;
	}

	public Result success(Boolean success) {
		this.setSuccess(success);
		return this;
	}

	public Result code(Integer code) {
		this.setCode(code);
		return this;
	}

	public Result msg(String msg) {
		this.setMsg(msg);
		return this;
	}

	public Result data(T data) {
		this.setData(data);
		return this;
	}

}
